import request from '@/utils/request'
import { setTokenInfo } from '@/utils/storage'
import { LOGIN_TOKEN } from '../contants/login'

/**
 * 短信验证码
 * @param {string} mobile
 * @returns thunk
 */
export const sendValidationCode = (mobile) => {
  return async () => {
    await request({
      url: `/sms/codes/${mobile}`,
      method: 'get',
    })
  }
}

/**
 * 登录
 * @param {} data
 * @returns
 */
export const login = (data) => {
  return async (dispatch) => {
    const res = await request({
      url: '/authorizations',
      method: 'post',
      data,
    })
    // console.log(res)
    dispatch(saveToken(res.data))
    // 存储token到本地
    setTokenInfo(res.data)
  }
}

// 存token到redux
export const saveToken = (payload) => ({ type: LOGIN_TOKEN, payload })
